import tkinter as tk
from color import *


class PuzzleCell:
    cellSize = 50
    margin = 25
    valueFont = ("Arial", int(cellSize * 0.7))
    candFont = ("Arial", int(cellSize / 3 * 0.8))

    def __init__(self, canvas: tk.Canvas, rowNum: int, colNum: int):
        self.canvas: tk.Canvas = canvas
        self.x0: int = colNum * self.cellSize + self.margin
        self.y0: int = rowNum * self.cellSize + self.margin

        self.generate()

    def generateBlock(self):
        self.block = self.canvas.create_rectangle(
            self.x0,
            self.y0,
            self.x0 + self.cellSize,
            self.y0 + self.cellSize,
            fill="white",
            outline="",
            state="hidden",
        )

    def generateValueCircle(self):
        center_x = self.x0 + self.cellSize / 2
        center_y = self.y0 + self.cellSize / 2
        radius = self.cellSize / 2 * 0.8
        self.valueCircle = self.canvas.create_oval(
            center_x - radius,
            center_y - radius,
            center_x + radius,
            center_y + radius,
            fill="white",
            outline="",
            state="hidden",
        )

    def generateCandCircles(self):
        self.candCircles = []
        for i in range(9):
            # 计算每个小格子的中心坐标
            center_x = self.x0 + (i % 3 + 0.5) * self.cellSize / 3
            center_y = self.y0 + (i // 3 + 0.5) * self.cellSize / 3
            # 以中心点为基准计算圆形边界
            radius = self.cellSize / 6 * 0.8
            c = self.canvas.create_oval(
                center_x - radius,
                center_y - radius,
                center_x + radius,
                center_y + radius,
                fill="white",
                outline="",
                state="hidden",
            )
            self.candCircles.append(c)

    def generateValue(self):
        x = self.x0 + self.cellSize / 2
        y = self.y0 + self.cellSize / 2
        self.textValue = self.canvas.create_text(
            (x, y),
            text="0",
            fill="black",
            font=PuzzleCell.valueFont,
            state="hidden",
        )

    def generateCands(self):
        self.textCands = []
        for i in range(9):
            x = self.x0 + i % 3 * self.cellSize / 3 + self.cellSize / 6
            y = self.y0 + i // 3 * self.cellSize / 3 + self.cellSize / 6
            c = self.canvas.create_text(
                (x, y),
                text=str(i + 1),
                font=self.candFont,
                fill="black",
                state="hidden",
                anchor="center",
            )
            self.textCands.append(c)

    def generate(self):
        self.generateBlock()
        self.generateValueCircle()
        self.generateCandCircles()
        self.generateValue()
        self.generateCands()

    def setValue(self, value, given=False):
        for c in self.textCands:
            self.canvas.itemconfig(c, state="hidden")
        if given:
            color = givenColor
        else:
            color = "black"
        self.canvas.itemconfig(
            self.textValue, text=str(value), fill=color, state="normal"
        )

    def setCands(self, cands):
        self.canvas.itemconfig(self.textValue, state="hidden")
        for i, c in enumerate(self.textCands, start=1):
            if i in cands:
                self.canvas.itemconfig(c, state="normal")
            else:
                self.canvas.itemconfig(c, state="hidden")
